package com.example.mymicaihu.fragment;

import android.util.Log;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.mvplibrary.ui.fragment.BaseMvpFragment;
import com.example.mymicaihu.R;
import com.example.mymicaihu.adapter.FragTabAdapter;
import com.example.mymicaihu.bean.HomeTabBean;
import com.example.mymicaihu.callback.Mainconcations;
import com.example.mymicaihu.fragment.home.FocusNewsFragment;
import com.example.mymicaihu.fragment.home.GameFragment;
import com.example.mymicaihu.fragment.home.HotNewsFragment;
import com.example.mymicaihu.fragment.home.NewsFragment;
import com.example.mymicaihu.fragment.home.OriginalFragment;
import com.example.mymicaihu.fragment.home.QuestionFragment;
import com.example.mymicaihu.fragment.home.VideoFragment;
import com.example.mymicaihu.model.MainModel;
import com.example.mymicaihu.presenter.MainPresenter;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：朱勇闯
 * data : 2021/4/10  14:30
 * 作用： xxxx
 */
public class HomeFragment<T> extends BaseMvpFragment<Mainconcations.MainView, MainModel, MainPresenter<T>> implements Mainconcations.MainView<T> {
    private static final String TAG = "朱勇闯Home";
    @BindView(R.id.tab_fragment_home)
    TabLayout tab_main;
    @BindView(R.id.vp_fragment_home)
    ViewPager viewPager;

    @Override
    public void showList(T t, int type) {
        if (type == 0) {
            Gson gson = new Gson();
            Type type1 = new TypeToken<List<HomeTabBean.DataBean>>() {
            }.getType();
            List<HomeTabBean.DataBean> tabBeans = gson.fromJson((JsonElement) t, type1);

            ArrayList<String> titles = new ArrayList<>();
            for (int i = 0; i < tabBeans.size(); i++) {
                titles.add(tabBeans.get(i).getTitle());
            }
            ArrayList<Fragment> fragments = new ArrayList<>();
            FocusNewsFragment focusNewsFragment = new FocusNewsFragment();
            NewsFragment newsFragment = new NewsFragment();
            GameFragment gameFragment = new GameFragment();
            OriginalFragment exclusivesFragment = new OriginalFragment();
            HotNewsFragment hotNewsFragment = new HotNewsFragment();
            VideoFragment videoFragment = new VideoFragment();
            QuestionFragment questionFragment = new QuestionFragment();

            fragments.add(focusNewsFragment);
            fragments.add(newsFragment);
            fragments.add(gameFragment);
            fragments.add(exclusivesFragment);
            fragments.add(hotNewsFragment);
            fragments.add(videoFragment);
            fragments.add(questionFragment);
            FragTabAdapter fragTabAdapter = new FragTabAdapter(getChildFragmentManager(), fragments, titles);
            Log.e(TAG, "initData: " + fragTabAdapter.toString());
            viewPager.setAdapter(fragTabAdapter);
            tab_main.setupWithViewPager(viewPager);

            viewPager.setCurrentItem(1);
        }
    }

    @Override
    public void showError(String error) {

    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }

    @Override
    protected MainPresenter<T> createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initEvent() {
        HashMap<String, Object> mapTab = new HashMap<>();
        mapTab.put("cv", "2.5.3");
        mapTab.put("ms", "MSMyLjUuMyMyMDIwMTIwMSNtYXJrZXRfdml2byMxI3Zpdm92aXZvIFg3I2ZmZmZmZmZmLWU4ZTQtMjIzZC1mZmZmLWZmZmZlNjRiYzYzYg");
        mapTab.put("referer", "http://api.micaiying.com");
        mPresenter.getData(mapTab, "http://api.micaiying.com/", "sys/channel", 0);
    }

    @Override
    protected int createLayoutId() {
        return R.layout.fragment_home;
    }
}
